#include<iostream>
#include<fstream>
using namespace std;

double newt(double x[], double y[], int n, double t)
{
	double difference[10][10] = { 0 };
	for (int i = 0; i < n; i++) {
		difference[i][0] = y[i];
	}
	for (int j = 1; j <n; j++) {
		for (int i = j; i < n; i++) {
			difference[i][j] = (difference[i][j - 1] - difference[i - 1][j - 1]) / (x[i] - x[i - j]);
		}
	}
	double sum = difference[0][0];
	for (int i = 1; i < n; i++) {
		double temp = 1;
		for (int j = 0; j < i; j++) {
			temp *= (t - x[j]);
		}
		sum = sum + difference[i][i] * temp;
	}
	return sum;
}

int main()
{
	double x[7] = { 0,6,10,13,17,20,28 };
	double s1[7] = { 6.67,17.3,42.7,37.3,30.1,29.3,28.7 };
	double s2[7] = { 6.67,16.1,18.9,15.0,10.6,9.44,8.89 };
	double k[57] = { 0 };
	double t1[57] = { 0 }, t2[57] = { 0 };
	for (int i = 0; i < 57; i++) {
		k[i] = 0.5 * i;
		t1[i] = newt(x, s1, 7, k[i]);
		t2[i] = newt(x, s2, 7, k[i]);
	}
	ofstream ocout;
	ocout.open("E.txt");
	for (int i = 0; i < 57; i++) {
		ocout << k[i] << " ";
		ocout << t1[i] << " ";
		ocout << t2[i] << endl;
	}
	ocout.close();

	double m1 = newt(x, s1, 7, 43);
	double m2 = newt(x, s2, 7, 43);
	if (m1 > 0) cout << "样本1中的幼虫不会消失" << endl;
	else cout << "样本1中的幼虫全部死亡" << endl;
	if (m2 > 0) cout << "样本2中的幼虫不会消失" << endl;
	else cout << "样本2中的幼虫全部死亡" << endl;
	return 0;
}